package net.cyue.simple.container.data;

import net.cyue.util.ReflectUtil;

public class InstanceWrapper {
    private static long INSTANCE_COUNT = 0;

    public final long id;
    public final String name;
    public MetaType metaType;
    private Object instance;

    public InstanceWrapper(
        MetaType metaType,
        String name,
        Object instance
    ) {
        this.metaType = metaType;
        this.id = ++InstanceWrapper.INSTANCE_COUNT;
        this.name = name;
        this.instance = instance;
        if (this.instance == null) {
            try {
                this.instance = this._new();
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
    }


    public void setInstance(Object instance) {
        this.instance = instance;
    }
    public Object getInstance() {
        return this.instance;
    }
    public Object _new() throws NoSuchMethodException {
        Class<?> clz = this.metaType.getClz();
        return ReflectUtil.createInstance(clz);
    }
}
